// page_two/zijianreport/zijianreport.js
let app = getApp()
let yuming = app.globalData.yuming
let shuzong = []
const request = require('../../config/request');

Page({
  ...request,

  /**
   * 页面的初始数据
   */
  data: {
    heights: "",
    gao: '',
    title: '自查报告',
    yuming: yuming,
    zong: [],
    isxuan: false,
    isxuans: false,
    study_status: null,
    date: ''
  },
  xuanz(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      study_status: index
    })
    if (index == 1) {
      this.setData({
        isxuan: true,
        isxuans: false,
      })
    }
    if (index == 2) {
      this.setData({
        isxuans: true,
        isxuan: false,
      })
    }
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  gojians(e) {
    let url = e.currentTarget.dataset.fil
    var that = this
    var s = url.lastIndexOf(".")
    var b = url.substring(s + 1) //下划线之后
    console.log(b)
    if (b == 'jpg' || b == 'jpeg' || b == 'png') {
      wx.previewImage({
        urls: [url]
      })
    } else {
      wx.showLoading({
        title: '正在打开中',
      })
      wx.downloadFile({
        url: url,
        success(res) {
          var filePath = res.tempFilePath
          console.log(url)
          wx.openDocument({
            filePath: filePath,
            fileType: b,
            success: function (res) {
              wx.hideLoading({
                success: (res) => {},
              })
            },
            fail(err) {
              console.log(err)
              wx.hideLoading({
                success: (res) => {},
              })
              wx.showToast({
                title: '打开失败',
                icon: 'none'
              })
            }
          })
        },
        fail(err) {
          console.log(err)
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showToast({
            title: '打开失败',
            icon: 'none'
          })
        }
      })
    }


  },
  shanchu(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    console.log(index)
    var zong = this.data.zong
    zong.splice(index, 1)
    this.setData({
      zong: zong
    })

  },
  gofujians() {
    shuzong = this.data.zong
    console.log(shuzong)
    let that = this
    var ar = []
    ar = ['xlsx', 'xls', 'doc', 'docx', 'pdf', "jpg", "jpeg", "png"]
    wx.chooseMessageFile({
      count: 1,
      type: 'all',
      extension: ar,
      success(res) {
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        var file = res.tempFiles[0].path
        var name = res.tempFiles[0].name
        console.log(file)
        var s = file.indexOf(".")
        var b = file.substring(s + 1) //下划线之后
        console.log(b)
        if (b != 'doc' && b != 'docx' && b != 'xls' && b != 'xlsx' && b != 'pdf' && b != 'jpg' && b != 'jpeg' && b != 'png') {
          wx.showToast({
            title: '当前格式不能上传哦~',
            icon: 'none'
          })
        } else {
          that.setData({
            type: b
          })
          wx.uploadFile({
            url: yuming + 'Besall/Upload',
            filePath: file,
            name: 'file',
            formData: {
              file: file
            },
            success(res) {
              var data = JSON.parse(res.data)
              var url = data.imgUrl
              var zongurl = data.httpUrl + data.imgUrl
              console.log(url)
              var obj = {
                name: name,
                appendix_url: url,
                zong: zongurl
              }
              shuzong.push(obj)
              that.setData({
                zong: shuzong
              })

              // that.setData({
              //   fujian: data.imgUrl
              // })
              // if (code2 == 'pdf' || code2 == 'docx') {
              // wx.downloadFile({
              //   url: data.httpUrl + data.imgUrl,
              //   success(res) {
              //     var filePath = res.tempFilePath
              //     that.setData({
              //       fils: filePath,
              //       zong: filePath

              //     })
              //   }
              // })
              // }

            }
          })
        }

      }
    })
  },
  tijiao() {
    var url = ''
    var data = {}
    var zong = JSON.stringify(this.data.zong)

    if (this.data.zong == '') {
      wx.showToast({
        title: '请选择附件',
        icon: 'none'
      })
      return
    }
    if (this.data.study_status == null) {
      wx.showToast({
        title: '请选择类型',
        icon: 'none'
      })
      return
    }
    url = 'Jinghexincheng/zijianbaogao_add'
    data = {
      image: zong,
      shopId: wx.getStorageSync('shopId'),
      year: this.data.date,
      zj_type: this.data.study_status
    }
    this.myRequest(yuming + url, data, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
    })
    this.setData({
      study_status: null,
      zong: []
    })
    var timeOut = setTimeout(() => {
      this.backs()
    }, 500)
  },
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    var dd = wuphoto.split(",")
    photo.splice(index, 1)
    dd.splice(index, 1)
    var rr = dd.join(",")
    this.setData({
      photo: photo,
      wuphoto: rr
    })
  },
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i], 1).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: wuphoto
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  bindyue(e) {
    if (e.detail.value > new Date().getFullYear()) {
      wx.showToast({
        title: '年份未到~',
        icon: 'none'
      })
      return
    } else {
      this.setData({
        date: e.detail.value
      })
      // this.getsmjlList();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      gao: wx.getMenuButtonBoundingClientRect().top + 40,
    })
    let date = new Date();
    let year = date.getFullYear();
    this.setData({
      // date: year + "-" + month + "-" + ri
      date: year
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})